package com.youku.expression.ast;

import com.youku.expression.Token;
import com.youku.expression.environment.Environment;

/**
 * while循环语句
 * 
 * @author loda
 *
 */
public class WhileLoopAst extends Ast {

	public WhileLoopAst(Token token, Ast... asts) {
		super(token, asts);
	}

	public WhileLoopAst(Ast... asts) {
		super(asts);
	}

	public Ast condition() {
		return getChild(0);
	}

	public Ast body() {
		return getChild(1);
	}

	@Override
	public String toString() {
		return new StringBuilder().append("while").append(condition()).append(body()).toString();
	}

	@Override
	public Object eval(Environment environment) {
		Object result = null;
		while (true) {
			boolean success = (boolean) condition().eval(environment);

			if (success) {
				result = body().eval(environment);
			} else {
				return result;
			}
		}
	}
}
